/**
 * @Author: tangy
 * @Date: 2020/2/23 16:55
 */
package com.cdxy.controller;

import com.cdxy.aop.ForceLogin;
import com.cdxy.aop.Token;
import com.cdxy.biz.process.ILoginBizProcess;
import com.cdxy.model.dto.req.*;
import com.cdxy.model.dto.base.controller.BaseController;
import com.cdxy.model.dto.base.resp.ApiResponse;
import com.cdxy.model.dto.resp.ImgCodeResp;
import com.cdxy.model.dto.resp.UserInfoResp;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


@RestController
@Api(tags = "登录接口")
public class LoginController extends BaseController {

    private ILoginBizProcess loginBizProcess;

    public LoginController(ILoginBizProcess loginBizProcess) {
        this.loginBizProcess = loginBizProcess;
    }

    @PostMapping("/sendLoginSms")
    @ApiOperation(value = "登录）发送短信验证码", notes = "传入短信发送验证码")
    public ApiResponse sendSms(@Validated @RequestBody SendMobileCodeReq sendMobileCodeReq) {
        loginBizProcess.sendSms(sendMobileCodeReq);
        return successApiResponse();
    }

    @PostMapping("/sendLoginEmail")
    @ApiOperation(value = "登录）发送邮箱", notes = "传入邮箱发送邮箱验证码")
    public ApiResponse sendEmail(@Validated @RequestBody SendEmailCodeReq sendEmailCodeReq) {
        loginBizProcess.sendEmail(sendEmailCodeReq);
        return successApiResponse();
    }

    @PostMapping("/verifyCode")
    @ApiOperation(value = "图片验证码", notes = "图片验证码")
    public ApiResponse<ImgCodeResp> verifyCode() {
        return successApiResponse(loginBizProcess.getImgCode());
    }

    @PostMapping("/loginPhoneCode")
    @ApiOperation(value = "手机验证码登录", notes = "手机验证码登录")
    @Token
    public ApiResponse<UserInfoResp> loginPhoneCode(@Validated @RequestBody LoginMobileCodeReq loginMobileCodeReq) {
        return successApiResponse(loginBizProcess.mobileCodeLogin(loginMobileCodeReq));
    }

    @PostMapping("/loginEmailCode")
    @ApiOperation(value = "邮箱验证码登录", notes = "邮箱验证码登录")
    @Token
    public ApiResponse<UserInfoResp> loginEmailCode(@Validated @RequestBody LoginEmailCodeReq loginEmailCodeReq) {
        return successApiResponse(loginBizProcess.emailCodeLogin(loginEmailCodeReq));
    }

    @PostMapping("/loginAccount")
    @ApiOperation(value = "账号密码登录", notes = "账号密码登录")
    @Token
    public ApiResponse<UserInfoResp> loginAccount(@Validated @RequestBody LoginAccountReq loginAccountReq) {
        return successApiResponse(loginBizProcess.accountLogin(loginAccountReq));
    }

    @PostMapping("/loginOut")
    @ApiOperation(value = "退出登录", notes = "")
    @ForceLogin
    public ApiResponse loginOut(@Validated @RequestBody UserWithLoginReq userWithLoginReq) {
        loginBizProcess.loginOut(userWithLoginReq);
        return successApiResponse();
    }
}
